package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.Part;
import com.efast.dmom.mes.bean.PartGroupItem;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.PartGroupItemDataHandler;
import com.efast.dmom.mes.datahandlers.entity.PartDataHandler;
import com.efast.dmom.mes.entity.PartGroupItemEntity;
import com.efast.dmom.mes.service.PartGroupItemService;

@Service
public class PartGroupItemServiceImpl implements PartGroupItemService{
	
	@Autowired
	private PartGroupItemDataHandler partGroupItemDataHandler;
	
	@Autowired
	private PartDataHandler partHandler;

	@Override
	public ResultBean save(PartGroupItem partGroupItem) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PartGroupItemEntity partGroupItemEntity = partGroupItem.getInnerData();
		if (partGroupItemEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空！");
			return resultBean;
		}
		if (partGroupItemEntity.getPartGroupId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请选择一条物料组进行新增！");
			return resultBean;
		}
		if (partGroupItemEntity.getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否选择！");
			return resultBean;
		}
		Part part = partHandler.getById(partGroupItemEntity.getPartId());
		if (part.getInnerData().getPlantId() != partGroupItem.getPlantId()) {
			resultBean.setSuccess(false);
			resultBean.setMessage("选择的物料与物料组中的工厂不是同一工厂,请重新选择！");
			return resultBean;
		}
		List<PartGroupItem> itemList = partGroupItemDataHandler.getByPartGroupId(partGroupItemEntity.getPartGroupId());
		if (itemList != null && itemList.size() > 0) {
		    for (PartGroupItem itemData : itemList) {
		    	if (itemData.getPart().getInnerData().getId() == partGroupItemEntity.getPartId()) {
		    		resultBean.setSuccess(false);
					resultBean.setMessage("选择的物料在该物料组下已存在,请重新选择！");
					return resultBean;
		    	}
		    }	
		}
		return partGroupItemDataHandler.save(partGroupItem);
	}

	@Override
	public ResultBean update(PartGroupItem partGroupItem) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PartGroupItemEntity partGroupItemEntity = partGroupItem.getInnerData();
		if (partGroupItemEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空！");
			return resultBean;
		}
		if (partGroupItemEntity.getPartGroupId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请选择一条物料组进行修改！");
			return resultBean;
		}
		if (partGroupItemEntity.getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否选择！");
			return resultBean;
		}
		PartGroupItem partGroupItemById = partGroupItemDataHandler.getById(partGroupItemEntity.getId());
		if (partGroupItemById.getInnerData().getPartId() != partGroupItemEntity.getPartId()) {
			Part part = partHandler.getById(partGroupItemEntity.getPartId());
			if (part.getInnerData().getPlantId() != partGroupItem.getPlantId()) {
				resultBean.setSuccess(false);
				resultBean.setMessage("选择的物料与物料组中的工厂不是同一工厂,请重新选择！");
				return resultBean;
			}
		List<PartGroupItem> itemList = partGroupItemDataHandler.getByPartGroupId(partGroupItemEntity.getPartGroupId());
		if (itemList != null && itemList.size() > 0) {
		    for (PartGroupItem itemData : itemList) {
		    	if (itemData.getPart().getInnerData().getId() == partGroupItemEntity.getPartId()) {
		    		resultBean.setSuccess(false);
					resultBean.setMessage("选择的物料在该物料组下已存在,请重新选择！");
					return resultBean;
		    	}
		    }	
		}
	  }
		return partGroupItemDataHandler.update(partGroupItem);
	}

	@Override
	public ResultBean delete(PartGroupItem partGroupItem) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PartGroupItemEntity partGroupItemEntity = partGroupItem.getInnerData();
		if (partGroupItemEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空！");
			return resultBean;
		}
		return partGroupItemDataHandler.delete(partGroupItem);
	}

	@Override
	public PartGroupItem getById(long id) {
		return partGroupItemDataHandler.getById(id);
	}

	@Override
	public ResultBean deleteBatch(List<PartGroupItem> groupItemList) {
		ResultBean resultBean = new ResultBean(true,' ');
		if (groupItemList == null || groupItemList.size() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请至少选择一条数据进行删除！");
			return resultBean;
		}
		return partGroupItemDataHandler.deleteBatch(groupItemList);
	}

}
